package com.tt.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tt.reggie.common.BaseContext;
import com.tt.reggie.common.R;
import com.tt.reggie.entity.ShoppingCart;
import com.tt.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    public ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> getList(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    @PostMapping("/add")
    public R<String> save(@RequestBody ShoppingCart shoppingCart){
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        if(shoppingCart==null){
            return R.error("加入购物车失败");
        }
        //条件
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();

        //判断添加到购物车的商品是菜品还是套餐
        if(shoppingCart.getDishId()!=null){
            //该东西为菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            wrapper.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        }else{
            //套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        //若购物车不存在此菜品/套餐，则添加，否则直接数量+1
        if(one==null){
            shoppingCartService.save(shoppingCart);
        }else{
            shoppingCart.setNumber(one.getNumber()+1);
            shoppingCart.setId(one.getId());
            shoppingCartService.updateById(shoppingCart);
        }
        return R.success("成功添加购物车！");

    }


    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        //判断是套餐还是菜品
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();

        if(shoppingCart.getSetmealId()==null){
            //为菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            if(shoppingCart.getDishFlavor()!=null){
                wrapper.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
            }else{
                return R.error("减少失败");
            }

        }else{
            //为套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if(one==null){
            return R.error("操作失败");
        }else if(one.getNumber()==1){
            shoppingCartService.remove(wrapper);
            return R.success("减少成功");
        }
        one.setNumber(one.getNumber()-1);
        shoppingCartService.updateById(one);
        return R.success("减少成功");
    }


    @DeleteMapping("/clean")
    public R<String> clean(){
        Long userId = BaseContext.getCurrentId();
        if (userId==null){
            return R.error("清空失败，请先登录！");
        }
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(wrapper);
        return R.success("清空成功！");
    }



}
